package org.john.guavatest;

import com.google.common.cache.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by adsame on 17-1-25.
 */
public class BlacklistCollecting {

    private Cache<IDAndParty, Integer> cache;

    public BlacklistCollecting(int maximumSize) {
        cache = CacheBuilder.newBuilder().
                maximumWeight(maximumSize).
                expireAfterWrite(3, TimeUnit.SECONDS).
                weigher(new Weigher<IDAndParty, Integer>() {
            public int weigh(IDAndParty key, Integer value) {
                return value;
            }
        }).build();
//                        new CacheLoader<IDAndParty, Integer>() {
//                    @Override
//                    public Integer load(IDAndParty key) throws Exception {
//                        return 1;
//                    }
    }

    public void insert(IDAndParty id) {
        Integer count = cache.getIfPresent(id);
        if (count == null) {
//            System.out.println("new insert");
            cache.put(id, 1);
        } else {
//            System.out.println("current vaule:" + count);
            cache.put(id, count + 1);
        }
    }

    public void print() {
        Map<IDAndParty, Integer> map = cache.asMap();
        System.out.println("size:" + map.size());
        System.out.print(map);
        System.out.println();
        System.out.print(map.keySet());
        System.out.println();
        int total = 0;
        for (IDAndParty id : map.keySet()) {
            if (cache.getIfPresent(id) >= 100) {
                System.out.print(id + ":" + cache.getIfPresent(id) + ";");
                ++total;
            }
        }
        System.out.println("\ncatch num:" + total);
    }

    public static void main(String args[]) {
        BlacklistCollecting cache = new BlacklistCollecting(10000);
        HashMap<IDAndParty, Integer> map = new HashMap<IDAndParty, Integer>();
        int prob = 1;
        Random ram = new Random();
        int rand;
        for (int i = 1; i <= 1000000; ++i) {
            if (ram.nextInt(100) < prob) {
                rand = ram.nextInt(10);
            } else {
                rand = ram.nextInt(1000000) + 10;
            }
//            System.out.print(rand + ";");
            IDAndParty idAndParty = new IDAndParty(String.valueOf(rand), "baidu");
            if (map.containsKey(idAndParty)) {
                map.put(idAndParty, map.get(idAndParty) + 1);
            } else {
                map.put(idAndParty, 1);
            }
            cache.insert(idAndParty);
        }
        System.out.println();
        System.out.println("map num:" + map.size() + "," + map);
        System.out.println("cache:");
        cache.print();
//        for (Object i : cache.keySet()) {
//            System.out.print(i + ":" + cache.frequencyOf(i) + ";");
//        }
//        for (int i = 0; i < 100; ++i) {
//            int freq = (Integer)cache.get(1);
//            cache.put(1, freq + 1);
//        }
    }
}
